import store from "@/store";
import http from "@/utils/http";
import { HttpResponse, checkHttpError } from "../common";
import { FORM_DATA_AXIOS_CONFIG } from "../constants";

/**
 * 获得该规则未分配的权限
 */
export async function getRoleNoPopdomList(rid: string): Promise<[]> {
  const form = new FormData();
  form.append("insys", store.getters.system.insysid);

  const resp: HttpResponse = await http.post(
    `/Role/${rid}/showrole_nopopdom2/`,
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  checkHttpError(resp);
  return resp.detail || [];
}

/**
 * 获得该规则已分配的权限
 */
export async function getRolePopdomList(rid: string): Promise<[]> {
  const form = new FormData();
  form.append("insys", store.getters.system.insysid);

  const resp: HttpResponse = await http.post(
    `/Role/${rid}/showrole_popdom2/`,
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  checkHttpError(resp);
  return resp.detail || [];
}

/**
 * 批量增加角色权限
 * @param {string} rid
 * @param {string[]} pids
 * @returns
 */
export async function batchAddPermsToRole(
  rid: string,
  pids: string[]
): Promise<string | boolean> {
  const form = new FormData();
  form.append("roleid", rid);
  form.append("popdomids", JSON.stringify(pids));

  const resp: HttpResponse = await http.post(
    "/Role/batchaddpopdoms/",
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  return checkHttpError(resp) ? resp.detail : false;
}

/**
 * 批量删除角色权限
 * @param {string} rid
 * @param {string[]} pids
 * @returns
 */
export async function batchDelPermsToRole(
  rid: string,
  pids: string[]
): Promise<string | boolean> {
  const form = new FormData();
  form.append("roleid", rid);
  form.append("popdomids", JSON.stringify(pids));

  const resp: HttpResponse = await http.post(
    "/Role/batchdelpopdoms/",
    form,
    FORM_DATA_AXIOS_CONFIG
  );

  return checkHttpError(resp) ? resp.detail : false;
}
